import { onMounted } from 'vue';
import { useIntervalFn } from '@vueuse/core';
import { Modal, message } from 'ant-design-vue';

const VERSION = '__version';

const useUpdateFrontend = () => {
  const localVersion = Number(localStorage.getItem(VERSION));
  if (NODE_ENV !== 'development') {
    const { pause } = useIntervalFn(() => {
      fetch('/version.json')
        .then((res) => res.json())
        .then((res) => {
          if (res.version > localVersion) {
            pause();
            localStorage.setItem(VERSION, res.version);
            Modal.info({
              title: '提示',
              content: '检测到项目更新，请刷新页面',
              okText: '刷新',
              onOk: () => {
                location.reload();
              },
            });
          }
        });
    }, 3000);
  }
};

export { useUpdateFrontend };
